<?php

namespace app\test\controller;

use think\Controller;
use think\Db;
use think\facade\Cache;
use think\facade\View;
use think\Request;

class Ele extends Controller
{
//跳转添加页面
    public function add()
    {
        return View::fetch('add');
    }
    //多图插件上传，接图
    public function addimg(Request $request)
    {
        $file=$request->file('file');
        $info=$file->move('./uploads');
        $img='/uploads/'.$info->getSaveName();
        return json($img);
    }
    //增加功能
    public function create(Request $request)
    {
       $arr['title']=$request->post('title');
       $arr['img']=$request->post('img');
       $arr['desc']=$request->post('desc');
       $arr['create_time']=time();
       if (Db::table('ele')->insert($arr)){
           return json(['code'=>200,'msg'=>'添加成功','data'=>$arr]);
       }else{
           return json(['code'=>400,'msg'=>'添加失败','data'=>null]);
       }
    }
//展示功能
    public function show(Request $request)
    {
        $data=Db::table('ele')->select();
        foreach($data as $k=>$v){//多图展示的一个循环处理
            $data[$k]['imgs']=explode(',',$v['img']);//字符转成数组
        }
        return View::fetch('show',['data'=>$data]);
    }
//异步按状态删除
    public function delete(Request $request)
    {
        $id=$request->post('id');
        $res=Db::table('ele')->where(['id'=>$id,'status'=>1])->delete();
        if ($res){
            return json(['code'=>200,'msg'=>'删除成功','data'=>$res]);
        }else{
            return json(['code'=>400,'msg'=>'删除失败','data'=>'']);
        }
    }
    //异步显示修改
    public  function update(Request $request)
    {
        $id=$request->post('id');
        $one=$request->post('one');
        if ($one==0){
            $one=1;
        }else{
            $one=0;
        }
        $res=Db::table('ele')->where('id',$id)->update(['status'=>$one]);
        if ($res){
            return json(['code'=>200,'msg'=>'修改成功','data'=>$res]);
        }else{
            return json(['code'=>400,'msg'=>'修改失败','data'=>'']);
        }
    }
}
